package com.ziyun.erp.web.api.goods;

import com.ziyun.erp.modules.goods.entity.GoodsTemplateEntity;
import com.ziyun.erp.modules.goods.service.GoodsTemplateService;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 商品模板表
 *
 * @author xueqz
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("port/goods/goodstemplate")
public class GoodsTemplateControllerApi {
    @Autowired
    private GoodsTemplateService goodsTemplateService;

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsTemplateService.queryPage(params);
        List<GoodsTemplateEntity> list = (List<GoodsTemplateEntity>) page.getList();
//        List<GoodsTemplateEntity> allTemplate = goodsTemplateService.selectAllTemplate();
        goodsTemplateService.setAttributeNames(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{templateId}")
    public R info(@PathVariable("templateId") Long templateId){
        GoodsTemplateEntity goodsTemplate = goodsTemplateService.selectById(templateId);
        ArrayList<GoodsTemplateEntity> goodsTemplateEntities = new ArrayList<>();
        goodsTemplateEntities.add(goodsTemplate);
        goodsTemplateService.setAttributeNames(goodsTemplateEntities);
        //查分类
        GoodsCategoryEntity goodsCategory = goodsCategoryService.getCatByTemplateId(templateId);
        return R.ok().put("goodsTemplate", goodsTemplate).put("goodsCategory",goodsCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsTemplateEntity goodsTemplate){
        goodsTemplateService.save(goodsTemplate);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsTemplateEntity goodsTemplate){
        ValidatorUtils.validateEntity(goodsTemplate);
        goodsTemplateService.updateAllColumnById(goodsTemplate);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] templateIds){
//        goodsTemplateService.deleteBatchIds(Arrays.asList(templateIds));
        goodsTemplateService.fagDeleteByTemplateIds(templateIds);
        return R.ok();
    }

}
